package com.yhg.qxmall.cartorder.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yhg.qxmall.cartorder.entity.FmsOrderItemEntity;
import com.yhg.qxmall.cartorder.service.FmsOrderItemService;
import com.yhg.qxmall.cartorder.vo.OrderItemVos;
import com.yhg.qxmall.cartorder.vo.OrderResult;
import com.yhg.qxmall.cartorder.vo.OrderStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yhg.qxmall.cartorder.entity.FmsOrderEntity;
import com.yhg.qxmall.cartorder.service.FmsOrderService;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.R;



/**
 * 订单
 *
 * @author 凌月青缃
 * @email 13@qxmall.com
 * @date 2020-12-22 14:08:53
 */
@RestController
@RequestMapping("cartorder/order")
public class FrontOrderController {
    @Autowired
    private FmsOrderService orderService;

    @Autowired
    private FmsOrderItemService orderItemService;

    /**
     * 修改订单状态
     */
    @GetMapping("/orderStatus/{orderId}/{status}")
    public R orderStatus(@PathVariable("orderId") Long orderId,@PathVariable("status") Integer status){
        boolean b = orderService.changeStatus(orderId,status);
        if (b){
            return R.ok().put("status",status);
        }else return R.error();
    }
    /**
     * 整个订单模块的功能需求
     * 1、前端将对象传过来
     * 2、为用户创建一个订单
     * 2.1、设置订单信息
     * 3、将购物车项目数据插入订单项目表
     * 4、封装数据放回给用户
     * createOrder
     */
    /**
     * 创建订单
     * @param vos
     * @return
     */
    @PostMapping("/createOrder")
    public R createOrder(@RequestBody OrderItemVos vos){
        //创建订单
        Long orderId = orderService.createOrder(vos);
        //返回判断
        if (orderId!=null){
            return R.ok().put("msg","订单创建成功！").put("orderId",orderId);
        }else return R.error().put("msg","订单创建失败！");
    }

    /**
     * 查询订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/orderItemList/{orderId}")
    public R orderItemList(@PathVariable("orderId") Long orderId){
        FmsOrderEntity orderEntity = orderService.getById(orderId);
        List<FmsOrderItemEntity> orderItemEntities = orderItemService.list(new QueryWrapper<FmsOrderItemEntity>()
                .eq("order_id", orderId));
        return R.ok().put("order",orderEntity).put("items",orderItemEntities);
    }


    /**
     * 查询所有订单
     */
    @PostMapping("/allOrderList")
//    public R allOrderList(@PathVariable("memberId") Long memberId,@PathVariable("status") Integer status){
        public R allOrderList(@RequestBody OrderStatusVo vo){
            List<FmsOrderEntity> memberOrder =new ArrayList<>();
        if (vo.getStatus()==null){
            //查出该用户所拥有的订单
            memberOrder = orderService.list(new QueryWrapper<FmsOrderEntity>().eq("member_id", vo.getMemberId()));

        }else {
            memberOrder = orderService.list(new QueryWrapper<FmsOrderEntity>().eq("member_id", vo.getMemberId()).eq("status",vo.getStatus()));
        }
//        Long i =0l;
        //流式编程一个购物单与很多购物项绑定
        Map<Long, OrderResult> orders = memberOrder.stream().collect(Collectors.toMap(k->k.getId(), v -> {
            OrderResult orderResult = new OrderResult();
            List<FmsOrderItemEntity> orderItem = orderItemService.list(new QueryWrapper<FmsOrderItemEntity>().eq("order_id", v.getId()));
            orderResult.setStatus(v.getStatus());
            orderResult.setOrderItem(orderItem);
            return orderResult;
        }));
        return R.ok().put("data",orders);

    }











    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		FmsOrderEntity fmsOrder = orderService.getById(id);

        return R.ok().put("fmsOrder", fmsOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FmsOrderEntity fmsOrder){
        orderService.save(fmsOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FmsOrderEntity fmsOrder){
        orderService.updateById(fmsOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
